import { message } from 'antd';
import * as caseServices from '../services/case';

const casefilter = {
  rt_version: '',
  project_name: '',
  assigne: '',
  dev_status: '',
  test_status: '',
  module: '',
};
export default {
  namespace: 'casemanage',
  state: {
    caseinfo: [],
    testreportinfo: [],
    projectlist: [],
    smokeresultfilter: {
      project_name: '',
      fix_version: '',
    },
  },
  reducers: { // 修改state
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
  effects: {
    *fetchCase({ payload: { params } }, { call, put }) {
      const { data } = yield call(caseServices.getCase, params);// 接口调用
      const errorMes = data.message || '查询失败';
      if (data.status !== 200) {
        message.error(errorMes);
      }
      yield put({
        type: 'save',
        payload: {
          caseinfo: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    *sendSmokeReport({ payload: { reportparam, cb } }, { call, put }) {
      const { data } = yield call(caseServices.sendSmokeReport, reportparam);
      const errorMes = data.msg || '发送失败';
      if (data.status === 200) {
        message.success('发送成功！');
        if (cb) cb();
      } else {
        message.error(errorMes);
      }
    },
    *updateCase({ payload: { updateparam, reset, cb } }, { call, put }) {
      const { data } = yield call(caseServices.updateCase, updateparam);
      const errorMes = data.message || '修改失败';
      if (data.status === 200) {
        // message.success('修改成功！');
        if (cb) cb();
        yield put({
          type: 'fetchCase',
          payload: {
            params: { ...casefilter },
          },
        });
      } else {
        if (reset) reset();
        message.error(errorMes);
      }
    },
    *deleteCase({ payload: { recordParams, cb } }, { call, put }) {
      const { data } = yield call(caseServices.deleteCase, recordParams);
      const errorMes = data.message || '删除失败';
      if (data.status === 200) {
        message.success('删除成功！');
        if (cb) cb();
        yield put({
          type: 'fetchCase',
          payload: {
            params: { ...casefilter },
          },
        });
      } else {
        message.error(errorMes);
      }
    },
    *downloadCaseTemplete({ payload: { cb } }, { call, put }) {
      const { data } = yield call(caseServices.DownloadTemplete);
      const errorMes = data.message || '下载失败';
      if (data.status === 200) {
        message.success('下载成功！');
        if (cb) cb();
      } else {
        message.error(errorMes);
      }
    },
    *fetchProjectList({ payload }, { call, put }) {
      const { data } = yield call(caseServices.GetProjectList);// 接口调用
      yield put({
        type: 'save',
        payload: {
          projectlist: data.data.objects || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
    *fetchSmokeResult({ payload: { params } }, { call, put }) {
      const { data } = yield call(caseServices.getSmokeResult, params);// 接口调用
      yield put({
        type: 'save',
        payload: {
          testreportinfo: data.data || [], // 逻辑表达式，前面为空则用后面的值
        },
      });
    },
  },

  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname.includes('/casemanage')) {
          dispatch({ type: 'fetchProjectList' });
        }
      });
    },
  },
};
